package com.eastday.biz.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;

import com.eastday.biz.ParseHtmlBiz;
import com.eastday.model.NewsInfoModel;
/**
 * 
 * @author dingxs
 *
 */
public class ParseHtmlBizImpl implements ParseHtmlBiz {
	
	private List<String> fetchPageUrlList;
	
	@Override
	public void parseHtmlToDb(List<String> pagelinkList, String url,String tempPath,String filename) {
		// TODO Auto-generated method stub
		boolean writeflag           = false; //写入标识符
		String breakpoint           = "";
		String filePath             = tempPath+"/"+filename;
		File file                   = new File(filePath);
		FileInputStream inputStream = null;
		BufferedReader br           = null;
		if(file.exists()){
			try{
				inputStream = new FileInputStream(file);
				br          = new BufferedReader(new InputStreamReader(inputStream));
				breakpoint  = br.readLine();
			}
			catch(IOException e){
				
			}
			finally{
				try{
					br.close();
					inputStream.close();
				}
				catch(IOException e){
					
				}
			}
		}
		for(String pagelink:pagelinkList){
			this.parseHtml(pagelink,url);
			
		}
	}
	
	private void parseHtml(String url,String source){
		List<NewsInfoModel> newsInfoModelList = new ArrayList<NewsInfoModel>();
		
	}

	/**
	 * 解析分页标签
	 */
	@Override
	public List<String> parseHtmlPageLink(String url,final String divid) throws ParserException {
		// TODO Auto-generated method stub
		fetchPageUrlList = new ArrayList<String>();
		int position          = url.lastIndexOf("/");
		String suffix         = url.substring(position+1);
		Parser parser         = new Parser();
		parser.setURL(url);
		NodeVisitor nodeVisitor = new NodeVisitor(){
			public void visitTag(Tag tag){
				if(tag.getAttribute("id")!=null && tag.getAttribute("id").equals(divid)){
					NodeList divChildren = tag.getChildren();//得到指定节点下的子节点
					for(int i = 0;i<divChildren.size();i++){
						Node nodeInDiv = divChildren.elementAt(i);
						if(nodeInDiv instanceof TagNode){//如果该节点是标签节点
							if(((TagNode)nodeInDiv).getTagName().toUpperCase().equals("P")){//如果检测到是P标签
								NodeList pChildren = nodeInDiv.getChildren();//得到该标签下的子节点
								for(int j = 0;j<pChildren.size();j++){
									Node nodeInP = pChildren.elementAt(j);
									if(((TagNode)nodeInP).getTagName().toUpperCase().equals("A")){
										Pattern pattern = Pattern.compile("^\\d*$");
										String pageNo   = nodeInP.getFirstChild().getText().trim();
										Matcher matcher = pattern.matcher(pageNo);
										if(matcher.find()){//如果是数字
											int position = ((TagNode)nodeInP).getAttribute("href").lastIndexOf("/")+1;
											fetchPageUrlList.add(((TagNode)nodeInP).getAttribute("href").substring(position));
										}
									}
								}
							}
						}
					}
				}
			}
		};
		if(nodeVisitor!=null){
			parser.visitAllNodesWith(nodeVisitor);
		}
		for(int i = 0;i<fetchPageUrlList.size();i++){
			String replace = url.replace(suffix, fetchPageUrlList.get(i));
			fetchPageUrlList.set(i, replace);
		}
		return fetchPageUrlList;
	}
	
	

}
